package top.lyf1210.yuwenpicturebackend.controller;

import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.COSObjectInputStream;
import com.qcloud.cos.utils.IOUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.lyf1210.yuwenpicturebackend.annotation.AuthCheck;
import top.lyf1210.yuwenpicturebackend.common.BaseResponse;
import top.lyf1210.yuwenpicturebackend.common.ResultUtils;
import top.lyf1210.yuwenpicturebackend.constant.UserAuthConstant;
import top.lyf1210.yuwenpicturebackend.exception.BusinessException;
import top.lyf1210.yuwenpicturebackend.exception.ErrorCode;
import top.lyf1210.yuwenpicturebackend.manager.CosManager;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

import static top.lyf1210.yuwenpicturebackend.constant.FileConstant.*;

/**
 * @ClassNameFileController
 * @Description TODO
 * @Author钰玟
 * @Date2025/4/17 下午1:19
 * @Version 1.0
 **/
@RestController
@Slf4j
//@Api(tags="文件测试接口")
public class FileController {

    @Resource
    private CosManager cosManager;

    @PostMapping("test/upload")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    @ApiOperation("文件上传")
    public BaseResponse<String> testUploadFile(@RequestParam("file") MultipartFile multipartFile) {
        //文件目录
        String fileName = multipartFile.getOriginalFilename();
        String filePath = String.format("/test/%s",fileName);
        File file = null;
        try {
            //上传文件
            file = File.createTempFile(filePath,null);
            multipartFile.transferTo(file);
            cosManager.putObject(filePath,file);
            //返回可访问文件
            return ResultUtils.success(filePath);
        } catch (IOException e) {
            log.error("file upload error,filePath:{}",filePath,e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,FAIL_UPLOAD);
        }finally {
            if(file !=null){
                //删除临时文件
                boolean delete = file.delete();
                if(!delete){
                    log.error("file delete error,filePath:{}",filePath);
                }
            }
        }
    }

    @GetMapping("/test/download/")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    @ApiOperation("文件下载")
    public void testDownload(String filePath, HttpServletResponse response) throws IOException {
        COSObjectInputStream cosObjectInputStream = null;
        try {
            COSObject cosObject = cosManager.getObject(filePath);
            cosObjectInputStream = cosObject.getObjectContent();
            //处理下载到的流
            byte[] bytes = IOUtils.toByteArray(cosObjectInputStream);
            //设置响应头
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition","attachment;filename="+filePath);

            //写入响应
            response.getOutputStream().write(bytes);
            response.getOutputStream().flush();
        } catch (IOException e) {
            log.error("download error,filePath:{}",filePath,e);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,FAIL_DOWNLOAD);
        }finally{
            if(cosObjectInputStream!=null){
                cosObjectInputStream.close();
            }
        }
    }

}
